package com.flute.icrawler.framework.framework.container;

public class NumberStringValue implements IStringValue {

	/**
	 * 
	 */
	private static final long serialVersionUID = 8625043424461559867L;
	private static final int LENGTH = 10;
	private Number number;

	public NumberStringValue(Number number) {
		this.number = number;
	}

	@Override
	public String toStringValue() {
		String stringValue = String.valueOf(number.intValue());
		int length = stringValue.length();
		for (int i = 0; i < LENGTH - length; i++) {
			stringValue = "0" + stringValue;
		}
		String decimalValue = String.valueOf(number.doubleValue()
				- number.intValue());
		stringValue = stringValue
				+ decimalValue.substring(1, Math.min(4, decimalValue.length()));

		return stringValue;
	}

	public static String numberToString(Number number) {
		String stringValue = String.valueOf(number.intValue());
		int length = stringValue.length();
		for (int i = 0; i < LENGTH - length; i++) {
			stringValue = "0" + stringValue;
		}
		String decimalValue = String.valueOf(number.doubleValue()
				- number.intValue());
		stringValue = stringValue
				+ decimalValue.substring(1, Math.min(4, decimalValue.length()));

		return stringValue;
	}

	public static Number stringToNumber(String string) {
		try {
			return Double.valueOf(string);
		} catch (Exception e) {
			return 0;
		}
	}

	public Number getNumber() {
		return number;
	}
	
	public String toString(){
		return String.valueOf(number);
	}
}
